package com.example.lending.View;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.Toolbar;

import com.example.lending.Model.Book;
import com.example.lending.Model.Borrow;
import com.example.lending.Model.User;
import com.example.lending.R;
import com.example.lending.ViewModel.BookViewModel;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * A simple {@link Fragment} subclass.
 */
public class AddBookFragment extends Fragment {
    BookViewModel bookViewModel;

    public AddBookFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_add_book, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        bookViewModel= ViewModelProviders.of(this).get(BookViewModel.class);
        final EditText edtBookCount,edtBookPrice,edtBookName,edtBookType;
        Button btnAdd;
        edtBookName=getView().findViewById(R.id.et_book_name);
        edtBookCount=getView().findViewById(R.id.et_book_count);
        edtBookPrice=getView().findViewById(R.id.et_book_price);
        edtBookType=getView().findViewById(R.id.et_book_type);
        btnAdd=getView().findViewById(R.id.btn_book_add);
        final Book book = new Book();
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = edtBookName.getText().toString();
                String type = edtBookType.getText().toString();
                String price = edtBookPrice.getText().toString();
                String count = edtBookCount.getText().toString();
                if(name.isEmpty()||type.isEmpty()||price.isEmpty()||count.isEmpty()){
                    Toast.makeText(getActivity(),"请填写全部数据",Toast.LENGTH_SHORT).show();
                }
                book.setName(name);
                book.setPrice(price);
                book.setType(type);
                book.setCount(count);
                try {
                    bookViewModel.insertBooks(book);
                    Toast.makeText(getActivity(), "添加成功", Toast.LENGTH_SHORT).show();
                }catch (Exception e){
                    e.printStackTrace();
                    Toast.makeText(getActivity(),"添加失败",Toast.LENGTH_SHORT).show();
                }
                NavController navController = Navigation.findNavController(v);
                navController.navigate(R.id.action_addBookFragment_to_homeFragment);

            }
        });

    }

}
